package com.xiake.consultsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiake.consultsystem.mapper.ConsultRecordMapper;
import com.xiake.consultsystem.mapper.CounselorMapper;
import com.xiake.consultsystem.mapper.StudentMapper;
import com.xiake.consultsystem.pojo.dto.CancelParam;
import com.xiake.consultsystem.pojo.po.Admin;
import com.xiake.consultsystem.mapper.AdminMapper;
import com.xiake.consultsystem.pojo.po.ConsultRecord;
import com.xiake.consultsystem.pojo.po.Student;
import com.xiake.consultsystem.pojo.vo.AdminRecordVo;
import com.xiake.consultsystem.service.AdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiake.consultsystem.pojo.vo.Result;
import com.xiake.consultsystem.utils.Constant;
import com.xiake.consultsystem.utils.RSAUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Mr.Chen
 * @since 2022-11-13
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    ConsultRecordMapper consultRecordMapper;
    /**
     * 管理员登录
     * @param account
     * @param password
     * @return
     */
    @Override
    public Result adminLogin(String account, String password) {
        //判断用户名密码是是否为空
        if(account==null || password==null || account.equals("") || password.equals("") ) {
           return  Result.fail(403,"账号或密码为空");
        }
        //判断用户名的正确性
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getAdminAccount, account);
        //先判断用户名是否存在
        Admin admin = adminMapper.selectOne(queryWrapper);
        if(admin==null) {
            return Result.fail(403,"用户名不存在");
        }

        //对传输过来的RSA密码用私钥进行解密
        //String rawPassword = RSAUtil.decode(password, Constant.PRIVATE_KEY);

        //对数据库中的取到密钥进行解密
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();

        //判断密码的正确性(开启RSA使用上一个判断)
        //if(bCryptPasswordEncoder.matches(rawPassword, admin.getAdminPassword()))
        if(bCryptPasswordEncoder.matches(password, admin.getAdminPassword())) {
            //登录成功,判断是否为超管
            if(admin.getIsRoot()) {
                System.out.println("超管登录成功");
                return Result.success("超管登录成功");
            } else {
                System.out.println("普通管理员登录成功");
                return Result.success("普通管理员登录成功");
            }
        } else {
            return Result.fail(403, "密码错误");
        }
    }
    /**
     * 根据输入的日期，咨询师id和学生姓名查询记录
     * @param date
     * @param counselorId
     * @param studentName
     * @return
     */
    @Override
    public Result showAvailableCounselor3(Date date, Long counselorId, String studentName) {
        if(counselorId ==null && studentName ==null){
            List<AdminRecordVo> list = adminMapper.adminshow1(date);
            return Result.success(list);
        }else if(counselorId != null && studentName ==null){
            List<AdminRecordVo> list = adminMapper.adminshow2(date, counselorId);
            return Result.success(list);
        }else if(counselorId == null && studentName != null){
            QueryWrapper<Student> wrapper1 = new QueryWrapper<Student>();
            wrapper1.eq("student_name",studentName);
            Student student = studentMapper.selectOne(wrapper1);
            if(student == null){
                return Result.fail(400,"你输入的学生没有预约信息");
            }
            List<AdminRecordVo> list = adminMapper.adminshow3(date, studentName);
            return Result.success(list);
        }else{
            QueryWrapper<Student> wrapper1 = new QueryWrapper<Student>();
            wrapper1.eq("student_name",studentName);
            Student student = studentMapper.selectOne(wrapper1);
            if(student == null){
                return Result.fail(400,"你输入的学生没有预约信息");
            }
            List<AdminRecordVo> list = adminMapper.adminshow4(date, counselorId, studentName);
            return Result.success(list);
        }
    }
    /**
     * 根据id来修改是否通知
     * @param cancelParam
     * @return
     */
    @Override
    public Result beinform(CancelParam cancelParam) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Long record_id = adminMapper.cancelrecord(simpleDateFormat.format(cancelParam.getDateTime()), cancelParam.getStudentName(), cancelParam.getCounselorName());
        ConsultRecord consultRecord = new ConsultRecord();
        consultRecord.setIsInform(1);
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("record_id",record_id);
        consultRecordMapper.update(consultRecord,wrapper);
        return Result.success("yes");
    }

    /**
     * 根据id来取消这次预约
     * @param cancelParam
     * @return
     */
    @Override
    public Result cancel(CancelParam cancelParam) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Long record_id = adminMapper.cancelrecord(simpleDateFormat.format(cancelParam.getDateTime()), cancelParam.getStudentName(), cancelParam.getCounselorName());
        ConsultRecord consultRecord = new ConsultRecord();
        consultRecord.setStudentId(0L);
        consultRecord.setAppointState(1L);
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("record_id",record_id);
        consultRecordMapper.update(consultRecord,wrapper);
        return Result.success("yes");
    }

}
